<template>
  <div class="app-container" v-loading="loading" element-loading-text="加载中...">
    <div class="app-container-inner">
      <el-form ref="ruleFormRef" :model="ruleForm" class="demo-ruleForm">
        <el-form-item label="厂商审核策略" prop="factoryCheckStrategy">
          <el-radio-group @change="onsaveFactorySet" v-model="ruleForm.factoryCheckStrategy">
            <el-radio :value="1">直接通过</el-radio>
            <el-radio :value="2">需要审核</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="报单余额策略" prop="orderBalanceStrategy">
          <el-radio-group @change="onsaveFactorySet" v-model="ruleForm.orderBalanceStrategy">
            <el-radio :value="1">不限制</el-radio>
            <el-radio :value="2">余额不足禁止下单</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted } from 'vue'
  import { getFactorySet, saveFactorySet } from '@/api/merchant'
  import { ElMessage } from 'element-plus'
  import pageComponentName from '@/config/page-component-name'

  defineOptions({
    name: pageComponentName.merchant.merchantSet,
  })

  const loading = ref(false)
  const ruleForm = ref({
    factoryCheckStrategy: 1, //	厂商审核策略：1-直接通过，2-需要审核,示例值(1)
    orderBalanceStrategy: 1, //报单余额策略：1-不限制/余额不足亦可下单，2-余额不足禁止下单,示例值(1)
  })
  const getFactoryDetailData = async () => {
    loading.value = true
    const res = await getFactorySet()
    if (res) {
      if (res.data.success) {
        ruleForm.value = res.data.data.row
      }
      if (!res.data.success) {
        if (res.data.isError)
          ElMessage({
            showClose: true,
            message: res.data.message,
            type: 'error',
          })
      }
    }
    loading.value = false
  }
  const onsaveFactorySet = async () => {
    loading.value = true
    const res = await saveFactorySet(ruleForm.value)
    if (res) {
      if (!res.data.success) {
        if (res.data.isError)
          ElMessage({
            showClose: true,
            message: res.data.message,
            type: 'error',
          })
      } else {
        await getFactoryDetailData()
      }
    }
    loading.value = false
  }
  onMounted(() => {
    getFactoryDetailData()
  })
</script>

<style scoped lang="scss">
  .app-container {
    height: 100%;

    .app-container-inner {
      display: flex;
      flex-direction: column;
      flex: 1;
    }
  }
</style>
